package com.caimiao.miao.library.utils.sys;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
//电池工具
public class BatteryUtil {

    public static boolean isConnect(Context context) {
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryIntent = context.registerReceiver(null, filter);
        int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
        int chargPlug = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbPlug = chargPlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acPlug = chargPlug == BatteryManager.BATTERY_PLUGGED_AC;
        ///电量在变化并且插头有插上
        return isCharging && (usbPlug || acPlug);
    }
}
